const fs = require('fs');
// 使用正则表达式匹配注释
const RemarkReg = /\/\*[^]*?\*\//g;

let markdown = '## 配置文件说明\n';

const readTSFiles = async () => {
    const tsFiles = await fs.promises.readdir('../../interfaces/configs/');
    const promises = tsFiles.map(async (tsFile) => {
        const tsData = await fs.promises.readFile(`../../interfaces/configs/${tsFile}`,'utf-8');
        return tsData
    })
    const tsResults = await Promise.all(promises);
    return tsResults
}

const readJsonFile = async (jsonName) => {
    const jsonFiles = await fs.promises.readdir('../../interfaces/xxx/');
    const promises = jsonFiles.map(async () => {
        const jsonData = await fs.promises.readFile(`../../interfaces/xxx/${jsonName}`,'utf-8');
        return jsonData
    })
    const jsonResults = await Promise.all(promises)
    return jsonResults
}

readTSFiles().then(tsResults => {
    tsResults.map((data,index)=>{
        const remarks = data.toString().match(RemarkReg)[0]
        if(remarks.length > 0){
            const remark = remarks.split('@')
            const newRemark = []
            let file = '';
            let title = '';
            let desc = '';
            let version = '';
            let cfgFile = '';
            remark.shift()
            remark.forEach((item)=>{
                let temp;
                temp = item.replace(/\r\n\s\D/g,"")
                newRemark.push(temp)
            })
            file = newRemark[2].split(' ')[1]
            title = newRemark[0].split(' ')[1]
            desc = newRemark[1].split(' ')[1]
            if(newRemark.length === 4){
                version = newRemark[3].split(' ')[1] 
                version = version.split('/')[0]
            }
            // console.log(file,title,desc,version);
            // 写入Markdown文件
            markdown += `### ${index+1}. ${file}(${title}) \n`;
            markdown += `<font color="#808080">${desc}</font> \n`;
            markdown +=  '```' + 'TypeScript\n' + data + '\n``` \n';
            
            // 读取json文件
            if(!version){
                cfgFile = file + '.json5';
                readJsonFile(cfgFile).then( jsonResults => {
                    markdown += `配置文件: ${cfgFile} \n`;
                    markdown += '```' + 'json\n' + jsonResults[jsonResults.length - 1] + '\n``` \n';
                    if(index + 1 === tsResults.length){
                        fs.writeFile('../../MarkDown/exportMd2.md',markdown,err => {
                            if (err) {
                                console.error(err);
                            } else {
                                console.log('Markdown文件生成成功！');
                            }
                        });
                    }
                })
            }else{
                cfgFile = file + '@' + version + '.json5';
                readJsonFile(cfgFile).then( jsonResults => {
                    markdown += `配置文件: ${cfgFile} \n`;
                    markdown += '```' + 'json\n' + jsonResults[jsonResults.length - 1] + '\n``` \n';
                    if(index + 1 === tsResults.length){
                        fs.writeFile('../../MarkDown/exportMd2.md',markdown,err => {
                            if (err) {
                                console.error(err);
                            } else {
                                console.log('Markdown文件生成成功！');
                            }
                        });
                    }
                })
            }
        }
    })
})

